﻿using System;

namespace QPEngine
{
	public static class TimeHelper
	{
		private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
		/// <summary>
		/// 客户端时间
		/// </summary>
		/// <returns></returns>
		public static long ClientNow()
		{
			return Convert.ToInt64((DateTime.UtcNow - epoch).TotalMilliseconds);
		}

		public static long ClientNowSeconds()
		{
			return Convert.ToInt64((DateTime.UtcNow - epoch).TotalSeconds);
		}

		public static long ClientNowTicks()
		{	
			return Convert.ToInt64((DateTime.UtcNow - epoch).Ticks);
		}

		/// <summary>
		/// 登陆前是客户端时间,登陆后是同步过的服务器时间
		/// </summary>
		/// <returns></returns>
		public static long Now()
		{
			return ClientNow();
		}
        /// <summary>
        /// 返回当前时间的字符串yyyymmddhhmmss
        /// </summary>
        public static string Format()
        {
            return DateTime.Now.ToString("yyyymmddhhmmss");
        }
        /// <summary>
        /// 获取当前的日期格式为 1970 01 01 00 00 00
        /// </summary>
        /// <returns></returns>
        public static long ToNumber()
        {
            string numberTime = string.Empty;
            string time = string.Empty;
            string zero = "0";
            time = DateTime.Now.Year.ToString();
            if (int.Parse(time) < 10) { time = zero + time; }
            numberTime += time;
            time = DateTime.Now.Month.ToString();
            if (int.Parse(time) < 10) { time = zero + time; }
            numberTime += time;
            time = DateTime.Now.Day.ToString();
            if (int.Parse(time) < 10) { time = zero + time; }
            numberTime += time;
            time = DateTime.Now.Hour.ToString();
            if (int.Parse(time) < 10) { time = zero + time; }
            numberTime += time;
            time = DateTime.Now.Minute.ToString();
            if (int.Parse(time) < 10) { time = zero + time; }
            numberTime += time;
            time = DateTime.Now.Second.ToString();
            if (int.Parse(time) < 10) { time = zero + time; }
            numberTime += time;
            return long.Parse(numberTime);
        }
    }
}